package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.UserDao;
import com.foreknow.elm.dao.impl.UserDaoImpl;
import com.foreknow.elm.po.User;
import com.foreknow.elm.service.UserSevice;
import com.foreknow.elm.util.DBUtil;

public class UserServiceImpl implements UserSevice {
    private int a;

    @Override
    public User getUserByIdByPass(String userId, String password) {
        User user = null;
        UserDao dao = new UserDaoImpl();
        try {
            DBUtil.getConnection();
            user = dao.getUserByIdByPass(userId, password);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return user;
    }

    @Override
    public int getUserById(String userId) {
        UserDao dao = new UserDaoImpl();
        try {
            DBUtil.getConnection();
            a = dao.getUserById(userId);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return a;
    }

    @Override
    public int saveUser(User user) {
        UserDao dao = new UserDaoImpl();
        try {
            // 开启事务
            DBUtil.beginTransaction();
            // 获取连接
            DBUtil.getConnection();
            a = dao.saveUser(user);
            // 提交事务
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                // 有异常时回滚事务
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return a;
    }
}
